package com.jason.order.service;


import com.jason.order.dao.TccOrderMapper;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class OrderTCCService implements IOrderTCCService{
    Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    TccOrderMapper tccOrderMapper;

    @Transactional(rollbackFor = Exception.class)
   public boolean prepare(BusinessActionContext businessActionContext,@BusinessActionContextParameter(paramName = "id") Integer id,
                           @BusinessActionContextParameter(paramName = "status") Integer status){
        logger.info("执行prepare");
        tccOrderMapper.updateStatusById(id,status);
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean commit(BusinessActionContext businessActionContext){
        logger.info("执行commit");
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean rollback(BusinessActionContext businessActionContext){
        logger.info("执行rollback");
        Integer id = businessActionContext.getActionContext("id",Integer.class);
        Integer status = businessActionContext.getActionContext("status",Integer.class);
        tccOrderMapper.updateStatusById(id,-status);
        return true;
    }
}
